package com.abocode.aichat.app.web;

import com.abocode.aichat.app.user.api.command.UpdateUserAccountCommand;
import com.abocode.aichat.app.user.api.command.UpdateUserCommand;
import com.abocode.aichat.app.user.api.command.UpdateUserPasswordCommand;
import com.abocode.aichat.app.user.model.User;
import com.abocode.aichat.app.user.service.UserService;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.abocode.aichat.app.utils.ResourceUtils;
import com.abocode.aichat.app.utils.StringUtil;
import com.abocode.aichat.app.vo.ResponseBuilder;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by guanxf on 2015/12/16.
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController  {
    @Autowired
    private UserService userService;
    @Autowired
    @NonNull
   private OSSClient ossClient;


    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Object update(@PathVariable Long id, @RequestBody @Valid UpdateUserCommand updateUserCommand) {
        userService.update(id, updateUserCommand);
        return ResponseBuilder.success();
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object get(@PathVariable Long id) {
        //    拼接字符串
        User user= userService.get(id);
        return ResponseBuilder.success(user);
    }


   @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Object delete(@PathVariable Long id) {
       userService.delete(id);
       return ResponseBuilder.success();
    }

    @RequestMapping(value = "/{id}/update-password", method = RequestMethod.PUT)
    public Object updatePassword(@PathVariable Long id, @RequestBody @Valid UpdateUserPasswordCommand command) throws Exception {
        userService.updatePassword(id, command);
        return ResponseBuilder.success();
    }


    @RequestMapping(value = "/{id}/update-account", method = RequestMethod.PUT)
    public Object updateAccount(@PathVariable Long id, @RequestBody @Valid UpdateUserAccountCommand command) throws Exception {
        userService.updateAccount(id, command);
        return ResponseBuilder.success();
    }

    @Value("${resource.public.url}")
    private String url;
    @RequestMapping(value = "/{id}/update-avatar", method = RequestMethod.POST)
    public Object updateAccount(@PathVariable Long id,@RequestParam MultipartFile file) throws Exception {
        String  originalFilename =file.getOriginalFilename();
        String  bucketName="teamnb-sns-img";
         boolean exists = ossClient.doesBucketExist(bucketName);
        if(!exists){
            ossClient.createBucket(bucketName);
        }
        String fileName=ResourceUtils.getFileName(originalFilename);
        final String fileUrl ="avatar/"+ ResourceUtils.getDateDir()+"/"+fileName;
        // 创建上传Object的Metadata
        ObjectMetadata meta = new ObjectMetadata();
        // 必须设置ContentLength
        meta.setContentLength(file.getSize());
        ossClient.putObject(bucketName, fileUrl, new ByteArrayInputStream(file.getBytes()), meta);
        String  avatar=userService.findOne(id).getAvatar();
        if(!StringUtil.isNullOrEmpty(avatar)){
            ossClient.deleteObject(bucketName,avatar);
        }
        userService.updateAvatar(id, fileUrl);
        Map map=new HashMap();
        map.put("avatar",url+fileUrl);
        return ResponseBuilder.success(map);
    }

}
